/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.DHTMLConstants;
import model.Vocabulary;

import org.myontology.users.Login;
import org.openrdf.model.URI;

import view.HTMLUIcomponents.BelongsToOM;
import view.HTMLUIcomponents.BroaderThan;
import view.HTMLUIcomponents.ClassesBelongingToOM;
import view.HTMLUIcomponents.ClassesSubclassOfClass;
import view.HTMLUIcomponents.Comment;
import view.HTMLUIcomponents.Domain;
import view.HTMLUIcomponents.Example;
import view.HTMLUIcomponents.Images;
import view.HTMLUIcomponents.InstancesHavingDomainClass;
import view.HTMLUIcomponents.Label;
import view.HTMLUIcomponents.NarrowerThan;
import view.HTMLUIcomponents.PartlyOverlapsWith;
import view.HTMLUIcomponents.PropertiesHavingDomainClass;
import view.HTMLUIcomponents.PropertiesHavingRangeClass;
import view.HTMLUIcomponents.Range;
import view.HTMLUIcomponents.SameAs;
import view.HTMLUIcomponents.SeeAlsos;
import view.HTMLUIcomponents.SubClassOf;
import view.HTMLUIcomponents.Synonyms;
import view.HTMLUIcomponents.Tags;
import view.HTMLUIcomponents.Translations;
import view.HTMLUIcomponents.Videos;
import controller.sesameDataConnector.AbstractDataConnector;

/**
 * Servlet implementation class for Servlet: GetFormInViewMode
 *
 */
 public class GetFormInViewMode extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 8L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public GetFormInViewMode() {
		super();
	}   	


	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		if ( !Login.LOGGED_IN.getValue(request)) {			
			response.sendRedirect( request.getContextPath() + "/login");
			return;
		}
		
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("UTF-8");
		
		String ret = "";

		String cssidcontent = request.getParameter( "cssidcontent" ).trim();
		String id = request.getParameter( "id" ).trim();
		
		String form = "";
		URI uri = null;

		try {

			// uri = new URIImpl( id );
			uri =AbstractDataConnector.getURI( id );
			
			if( cssidcontent.equals( Comment.CSSIDCONTENT ) ) {

				form = Comment.getFormInViewMode(uri, true, true, Vocabulary.ENGLISH );
			}
			else if( cssidcontent.equals( Tags.CSSIDCONTENT ) ) {
					
				form = Tags.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( Synonyms.CSSIDCONTENT ) ) {
					
				form = Synonyms.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( SeeAlsos.CSSIDCONTENT ) ) {
					
				form = SeeAlsos.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( Example.CSSIDCONTENT ) ) {
					
				form = Example.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( Translations.CSSIDCONTENT ) ) {
					
				form = Translations.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( Images.CSSIDCONTENT ) ) {
					
				form = Images.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( Videos.CSSIDCONTENT ) ) {
					
				form = Videos.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( ClassesBelongingToOM.CSSIDCONTENT ) ) {
					
				form = ClassesBelongingToOM.getFormInViewMode(uri, true, true );
			}
			/*
			else if( cssidcontent.equals( PropertiesBelongingToOM.CSSIDCONTENT ) ) {
					
				form = PropertiesBelongingToOM.getFormInViewMode(uri, true );
			}
			*/
			else if( cssidcontent.equals( PartlyOverlapsWith.CSSIDCONTENT ) ) {

				URI type = AbstractDataConnector.getType(uri);
				if( type.equals(Vocabulary.CLASS) ) {
					
					form = PartlyOverlapsWith.getFormInViewMode(uri, DHTMLConstants.CLASS_VIEWTYPE, true, true );
				}
				else if( type.equals(Vocabulary.DATATYPEPROPERTY) || type.equals(Vocabulary.OBJECTPROPERTY)) {
					
					form = PartlyOverlapsWith.getFormInViewMode(uri, DHTMLConstants.PROPERTY_VIEWTYPE, true, true );					
				}
			}
			else if( cssidcontent.equals( SameAs.CSSIDCONTENT ) ) {

				URI type = AbstractDataConnector.getType(uri);
				if( type.equals(Vocabulary.CLASS) ) {
					
					form = SameAs.getFormInViewMode(uri, DHTMLConstants.CLASS_VIEWTYPE, true, true );
				}
				else if( type.equals(Vocabulary.DATATYPEPROPERTY) || type.equals(Vocabulary.OBJECTPROPERTY)) {
					
					form = SameAs.getFormInViewMode(uri, DHTMLConstants.PROPERTY_VIEWTYPE, true, true );					
				}
			}
			else if( cssidcontent.equals( NarrowerThan.CSSIDCONTENT ) ) {

				URI type = AbstractDataConnector.getType(uri);
				if( type.equals(Vocabulary.CLASS) ) {
					
					form = NarrowerThan.getFormInViewMode(uri, DHTMLConstants.CLASS_VIEWTYPE, true, true );
				}
				else if( type.equals(Vocabulary.DATATYPEPROPERTY) || type.equals(Vocabulary.OBJECTPROPERTY)) {
					
					form = NarrowerThan.getFormInViewMode(uri, DHTMLConstants.PROPERTY_VIEWTYPE, true, true );					
				}
			}
			else if( cssidcontent.equals( BroaderThan.CSSIDCONTENT ) ) {

				URI type = AbstractDataConnector.getType(uri);
				if( type.equals(Vocabulary.CLASS) ) {
					
					form = BroaderThan.getFormInViewMode(uri, DHTMLConstants.CLASS_VIEWTYPE, true, true );
				}
				else if( type.equals(Vocabulary.DATATYPEPROPERTY) || type.equals(Vocabulary.OBJECTPROPERTY)) {
					
					form = BroaderThan.getFormInViewMode(uri, DHTMLConstants.PROPERTY_VIEWTYPE, true, true );					
				}
			}
			else if( cssidcontent.equals( SubClassOf.CSSIDCONTENT ) ) {
					
				form = SubClassOf.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( Domain.CSSIDCONTENT ) ) {
					
				form = Domain.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( Range.CSSIDCONTENT ) ) {
					
				form = Range.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( BelongsToOM.CSSIDCONTENT ) ) {
					
				form = BelongsToOM.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( ClassesSubclassOfClass.CSSIDCONTENT ) ) {
					
				form = ClassesSubclassOfClass.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( PropertiesHavingDomainClass.CSSIDCONTENT ) ) {
					
				form = PropertiesHavingDomainClass.getFormInViewMode(uri, true, true );
			}
			
			else if( cssidcontent.equals( InstancesHavingDomainClass.CSSIDCONTENT ) ) {
				
				form = InstancesHavingDomainClass.getFormInViewMode(uri, true, true );
			}
			
			else if( cssidcontent.equals( PropertiesHavingRangeClass.CSSIDCONTENT ) ) {
					
				form = PropertiesHavingRangeClass.getFormInViewMode(uri, true, true );
			}
			else if( cssidcontent.equals( Label.CSSIDCONTENT ) ) {

				form = "";
			}
			else if( cssidcontent.equals( Label.CSSIDCONTENT_TITLE ) ) {

				form = Label.getTitleForm( uri, AbstractDataConnector.getType(uri), true );
			}
			
			
		}
		catch( Exception e ) {
			
			e.printStackTrace();
		}
		

		ret = "<![CDATA[" + form + "]]>";
		

		// --------------------------

		ret = 
			"<handlegetforminviewmoderesponse>" + 
				"<" + cssidcontent + ">" + 
					ret + 
				"</" + cssidcontent + ">" + 
			"</handlegetforminviewmoderesponse>";
		
	    PrintWriter out = response.getWriter();
		
	    out.println("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>");
	    
	    out.println( ret );
	}  	  	  	     	  	  	    
}
